/*
 * Created by: Jonathan Messer
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using Microsoft.Win32; // to access registry
using System.Diagnostics; // to run programs
using System.Runtime.InteropServices; // to use time functions
using System.Configuration; // to manage config files
using System.IO; // to manage reading and writing files



                
namespace time
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Width = 457; // sets the width of the program
            this.Height = 21; // sets the height of the program, to hide the options menu
            DesktopLocation = new System.Drawing.Point (100,1); // sets the starting location of the program, top left of screen
            this.StartPosition = FormStartPosition.Manual; // allows the start position to be set manually
            groupBox1.Visible = false; // hides the groupbox menu, which is my options menu

            RegistryKey mykey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", false); // tells where next line should look in registry
            string myvalue = (string)mykey.GetValue("TitleBarTime"); // checks whether the program is set to run on startup
            if (myvalue == "") // if the program is not set to run on startup 
            {
                chkStartup.Checked = false; // uncheck the box for run on start up
            }
            else // else, if it is set to run on start up
            {
                chkStartup.Checked = true; // check the box to run on startup
            }

            ColorConverter cnv = new ColorConverter(); // method set to convert strings into System colors

            if (File.Exists(Application.StartupPath + @"\time.exe.config")) // checks if the config file exists
            {
                // Sets the label color based on config file setting, uses the cnv method to convert the string into a system color
                label1.ForeColor = (System.Drawing.Color)cnv.ConvertFromString(ConfigurationManager.AppSettings["foreground"]);
                label2.ForeColor = (System.Drawing.Color)cnv.ConvertFromString(ConfigurationManager.AppSettings["foreground"]);
                lblOptionOne.ForeColor = (System.Drawing.Color)cnv.ConvertFromString(ConfigurationManager.AppSettings["foreground"]);
                lblOptionTwo.ForeColor = (System.Drawing.Color)cnv.ConvertFromString(ConfigurationManager.AppSettings["foreground"]);
                cmbColors.Text = (ConfigurationManager.AppSettings["foreground"]); // sets the combo box to the foreground color, this prevents problems in the future
            }
            else // if no config file is found...
            {
                // alert user no config file is found, and settings will revert to default
                MessageBox.Show("ERROR: No config file found, all values set to default");
            }

            // Sets the default options, including combo box items, and check box settings
            cmbColors.Items.Add("White");
            cmbColors.Items.Add("Black");
            cmbColors.Items.Add("Red");
            cmbColors.Items.Add("Blue");

            chkOnline.Checked = true;
            chkTime.Checked = true;

            // starts the core of the program
            Date();
        }

        // variables to be used in multiple methods
        DateTime startTime;
        TimeSpan stoppedTime;
        int position = 100;

        // when the form loads, set set the start time to the current time, and enable the timer2
        private void Form1_Load(object sender, EventArgs e)
        {
            startTime = DateTime.Now;
            stoppedTime = new TimeSpan(0);
            timer2.Enabled = true;
        }

        // if the box current time is checked, display the curent time
        private void timer_Tick(object sender, EventArgs e)
        {
            if (chkTime.Checked == true)
            {
                CurrentTime();
            }
        }

        // if the box online time is checked, display the online time
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (chkOnline.Checked == true)
            {
                string online = OnlineTime();
                lblOptionTwo.Text = (online);
            }
        }

        // Exits the application if the exit button is clicked
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // shows the option menu, stretching the form, and making the group box visible
        private void button2_Click(object sender, EventArgs e)
        {
            this.Height = 103;
            groupBox1.Visible = true;
        }

        // checks the various settings in if/else statements, and changes the settings accordingly
        private void btnApply_Click(object sender, EventArgs e)
        {
            if (cmbColors.SelectedItem == "White") // if the combobox has the color set to white
            {
                button1.ForeColor = Color.White;
                button2.ForeColor = Color.White;
                lblOptionTwo.ForeColor = Color.White;
                lblOptionOne.ForeColor = Color.White;
                label1.ForeColor = Color.White;
                label2.ForeColor = Color.White;
                groupBox1.Visible = false;
            }
            else if (cmbColors.SelectedItem == "Red") // if the combobox has the color set to red
            {
                button1.ForeColor = Color.Red;
                button2.ForeColor = Color.Red;
                lblOptionTwo.ForeColor = Color.Red;
                lblOptionOne.ForeColor = Color.Red;
                label1.ForeColor = Color.Red;
                label2.ForeColor = Color.Red;
                groupBox1.Visible = false;
            }
            else if (cmbColors.SelectedItem == "Black") // if the combobox has the color set to black
            {
                button1.ForeColor = Color.Black;
                button2.ForeColor = Color.Black;
                lblOptionTwo.ForeColor = Color.Black;
                lblOptionOne.ForeColor = Color.Black;
                label1.ForeColor = Color.Black;
                label2.ForeColor = Color.Black;
                groupBox1.Visible = false;
            }
            else if (cmbColors.SelectedItem == "Blue") // if the combobox has the color set to blue
            {
                button1.ForeColor = Color.Blue;
                button2.ForeColor = Color.Blue;
                lblOptionTwo.ForeColor = Color.Blue;
                lblOptionOne.ForeColor = Color.Blue;
                label1.ForeColor = Color.Blue;
                label2.ForeColor = Color.Blue;
                groupBox1.Visible = false;
            }

            if (chkTime.Checked == true && chkOnline.Checked == true && chkDate.Checked == false) // checks for current and online time checked
            {
                label1.Text = "Online Time:";
                CurrentTime();

                label2.Text = "Current Time:";
                OnlineTime();
            }
            else if (chkTime.Checked == true && chkDate.Checked == true && chkOnline.Checked == false) // chekcs for curent time and date checked
            {
                label2.Text = "Current Time:";
                CurrentTime();

                label1.Text = "Date:";
                lblOptionTwo.Text = Date();
            }
            else if (chkOnline.Checked == true && chkDate.Checked == true && chkTime.Checked == false) // checks for online time and date checked
            {
                label2.Text = "Date:";
                lblOptionOne.Text = Date();

                label1.Text = "Online:";
                OnlineTime();
            }
            else // if 1 or 3 options selected
            {
                MessageBox.Show("Sorry, please choose two options");
            }

            if (chkSolidBackground.Checked == true) // if the solid background button is checked
            {
                this.BackColor = Color.Black;
            }
            else
                this.BackColor = Color.Gold; // gold is the transparent color, making the background transparent

            if (chkStartup.Checked == true) // if the checkbox run on startup is checked
            {
                // checks if the registry folder for run on startup exists, hopefully it should or you have problems
                RegistryKey mykey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                // creates a new key, labeled TitleBarTime with the value of the application startup path
                mykey.SetValue("TitleBarTime", Application.StartupPath + @"\time.exe", RegistryValueKind.String);
            }
            else // if the checkbox for the run on startup is not checked
            {
                // checks for the run on startup folder of the registry
                RegistryKey mykey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                // sets the value of the TitleBarTime to blank, to not run on startup
                mykey.SetValue("TitleBarTime", "", RegistryValueKind.String);
            }

            this.Height = 21; // sets the height back to default size, hiding the menu
            groupBox1.Visible = false; // hides the group box
        }

        private void btnRight_Click(object sender, EventArgs e) // allows the form to be moved to the right
        {
            position = position + 50; // gets the current position, and adds 50 to it
            DesktopLocation = new System.Drawing.Point(position, 1); // moves the program
            ResetMouseEventArgs(); // resets mouse event, so button can be clicked again
 
        }

        private void btnLeft_Click(object sender, EventArgs e) // allows the form to be moved to the left
        {
            position -= 50; // gets the current postion, and subtracts 50
            DesktopLocation = new System.Drawing.Point(position, 1); // moves the program
            ResetMouseEventArgs(); // resets mouse event, so button can be clicked again
        }

        private void CurrentTime() // heh, this is a long one, sets the current time
        {
            string hh, mm, ss; 

            hh = Convert.ToString(DateTime.Now.Hour); // hours 
            mm = Convert.ToString(DateTime.Now.Minute); // minutes 
            ss = Convert.ToString(DateTime.Now.Second); // seconds

            if (DateTime.Now.Second < 10) // if the seconds is less than 10, add a 0 before it
            {
                ss = "0" + ss;
            }
            if (DateTime.Now.Minute < 10) // if the seconds is less than 10, add a 0 before it
            {
                mm = "0" + mm;
            }
            
            //adds AM or PM to the time
            if (DateTime.Now.Hour < 11)
            {
                lblOptionOne.Text = (hh + ":" + mm + ":" + ss + " AM");
            }
            else if (DateTime.Now.Hour == 12)
            {
                lblOptionOne.Text = (hh + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 13)
            {
                lblOptionOne.Text = ("1" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 14)
            {
                lblOptionOne.Text = ("2" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 15)
            {
                lblOptionOne.Text = ("3" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 16)
            {
                lblOptionOne.Text = ("4" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 17)
            {
                lblOptionOne.Text = ("5" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 18)
            {
                lblOptionOne.Text = ("6" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 19)
            {
                lblOptionOne.Text = ("7" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 20)
            {
                lblOptionOne.Text = ("8" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 21)
            {
                lblOptionOne.Text = ("9" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 22)
            {
                lblOptionOne.Text = ("10" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 23)
            {
                lblOptionOne.Text = ("11" + ":" + mm + ":" + ss + " PM");
            }
            else if (DateTime.Now.Hour == 24)
            {
                lblOptionOne.Text = ("12" + ":" + mm + ":" + ss + " AM");
            }
        }

        // determines the online time of the computer
        private string OnlineTime()
        {
            TimeSpan runningTime;
            TimeSpan totalTime;

            string hh, mm, ss;

            totalTime = DateTime.Now - startTime;
            runningTime = totalTime - stoppedTime;

            TimeSpan t = runningTime;

            hh = t.Hours.ToString();
            mm = t.Minutes.ToString();
            ss = t.Seconds.ToString();

            if (t.Minutes < 10)
            {
                mm = ("0" + mm);
            }
            if (t.Seconds < 10)
            {
                ss = ("0" + ss);
            }

            string temp =(hh + ":" + mm + ":" + ss);
            return temp;
        }

        // sets the date
        private string Date()
        {
            string day = DateTime.Now.DayOfWeek.ToString();
            string date = DateTime.Now.Day.ToString();
            string month = DateTime.Now.Month.ToString();
            string year = DateTime.Now.Year.ToString();

            if (month == "1")
            {
                month = "January";
            }
            else if (month == "2")
            {
                month = "February";
            }
            else if (month == "3")
            {
                month = "March";
            }
            else if (month == "4")
            {
                month = "April";
            }
            else if (month == "5")
            {
                month = "May";
            }
            else if (month == "6")
            {
                month = "June";
            }
            else if (month == "7")
            {
                month = "July";
            }
            else if (month == "8")
            {
                month = "August";
            }
            else if (month == "9")
            {
                month = "September";
            }
            else if (month == "10")
            {
                month = "October";
            }
            else if (month == "11")
            {
                month = "November";
            }
            else if (month == "12")
            {
                month = "December";
            }

            string temp = (day + " " + month + " " + date + "," + year);
            return temp;
            
        }

        // picture as a button
        private void picNotepad_Click(object sender, EventArgs e)
        {
            if ((Control.ModifierKeys & Keys.Control) == Keys.Control ) // if the user holds control and clicks the picture
            {
                Process.Start("calc");  // starts the windows calculator, "calc" is the shortcut in windows
            }
            else // or if the user clicks the button
            {
                notepad frm = new notepad(position); // declares a new instance of the notepad form
                frm.Show(); // shows the form notepad

            }
        }

        private void btnSave_Click(object sender, EventArgs e) // this method will save the settings into the config file
        {
            StreamWriter sw = new StreamWriter(Application.StartupPath + @"\time.exe.config"); // new instance of the stream writer
            sw.WriteLine("<configuration>");

            sw.WriteLine("<appSettings>");

            sw.WriteLine("<add key=\"foreground\" value=\"" + cmbColors.SelectedItem + "\" />");

            sw.WriteLine("</appSettings>");

            sw.WriteLine("</configuration>");

            sw.Close(); // closes the stream writer
        }
        }


    }